package org.example.forum.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.confing.AppConfig;
import org.example.forum.model.Article;
import org.example.forum.model.ArticleReply;
import org.example.forum.model.User;
import org.example.forum.services.IArticleReplyService;
import org.example.forum.services.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name="回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArticleReplyService articleReplyService;
    @Operation(summary = "回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(description = "帖子id") @RequestParam(value ="articleId") @NonNull Long articleId,
                            @Parameter(description = "回复内容") @RequestParam(value = "content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //帖子要回复的对象
        Article article = articleService.selectById(articleId);
        if(article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if(article.getState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNEO);
        }
        if(user.getId()==article.getId()){
            return AppResult.failed("不能回复自己的帖子");
        }
        //构建回复对象
        ArticleReply articleReply=new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());//回复的发送者
        articleReply.setContent(content);
        //写入回复
        articleReplyService.create(articleReply);
        return AppResult.success();
    }
    @Operation(summary = "查询回复帖子")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@Parameter(description = "帖子id") @RequestParam(value = "articleId")@NonNull Long articleId){
        //校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if(article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }

}
